$(document).ready(function(){
	getCoupon();
});

function getCoupon(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/coupon/get-coupon',
            type:'post',
            data:function(d){
            	d.mobile = $('#mobile').val();
            	d.sms_verification = $('#sms_verification').val();
            	d.is_exchange = $('#is_exchange').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "id", "bSortable": false},
            { "data": "title", "bSortable": false },
            { "data": "coupon_time", "bSortable": false },
            { "data": "is_exchange", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [3],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	var html = '<span class="text-'+(data == 1 ? 'success' : 'danger')+'">'+(data == 1 ? '已' : '未')+'发放</span>';
                	return html+'<input name="is_exchange" value="'+data+'" type="hidden"/>';
                }
            }
        ]
    });
}

function table_list_reload(){
	var table = $('#table_list').DataTable();
    table.ajax.reload();
}

function grantCoupon(){
	var mobile = $('#mobile_2').val();
	var sms_verification = $('#sms_verification_2').val();
	if(mobile == '' || sms_verification == ''){
		$.jBox.error('发放优惠券前请先进行手机号和优惠验证码的搜索', '错误');
		return false;
	}
	$.post('/coupon/grant-coupon',{mobile:mobile,sms_verification:sms_verification},success,'json');
	return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
        	$.jBox.tip(data.val, 'success');
        	window.setTimeout(function () {
        		$('#coupon-modal').modal('hide');
        		 table_list_reload();
        	}, 1000);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
}

$('#reset_button').click(function(){
	$('#mobile').val('');
	$('#sms_verification').val('');
	$('#is_exchange').val(0);
    table_list_reload();
});

$('#coupon_id').on('click',function(){
	$('input[name="coupon_id[]"]').prop('checked',$(this).prop('checked'));
});

$('.grant_coupon').on('click',function(){
	$('#mobile_2').val('');
	$('#sms_verification_2').val('');
	$('#table_list_2').find('tbody').html('');
	$('#coupon-modal').modal('show');
});

$('.get-grant-coupon').on('click',function(){
	var mobile = $('#mobile_2').val();
	var sms_verification = $('#sms_verification_2').val();
	if(mobile == '' || sms_verification == ''){
		$.jBox.error('搜索优惠券前请先进行手机号和优惠验证码的搜索', '错误');
		return false;
	}
	$.post('/coupon/get-grant-coupon',{mobile:mobile,sms_verification:sms_verification},success,'json');
	return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
        	var result = data.val;
        	var html = '';
        	if(result.length > 0){
        		result.forEach(function(e){
        			var exchange_html = '<span class="text-'+(e.is_exchange == 1 ? 'success' : 'danger')+'">'+(e.is_exchange == 1 ? '已' : '未')+'发放</span>';
        			html += '<tr>';
        			html += '<td>'+e.title+'<input name="coupon_receive_id[]" value="'+e.id+'" type="hidden"/></td>';
        			html += '<td>'+e.coupon_time+'</td>';
        			html += '<td>'+exchange_html+'</td>';
        			html += '</tr>';
        		});
        	}
        	$('#table_list_2').find('tbody').html(html);
        }else{
        	$('#table_list_2').find('tbody').html('');
            $.jBox.error(data.val, '错误');
        }
    }
});